package com.kalix.dataPlatform.right.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.dataPlatform.right.api.biz.IWaringLevelBeanService;
import com.kalix.dataPlatform.right.api.dao.IWaringLevelBeanDao;
import com.kalix.dataPlatform.right.api.model.InfUtil;
import com.kalix.dataPlatform.right.entities.WaringLevelBean;
import com.kalix.framework.core.util.ConfigUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class WaringLevelBeanServiceImpl extends GenericBizServiceImpl<IWaringLevelBeanDao, WaringLevelBean> implements IWaringLevelBeanService {
    public static String Ip = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String Port =(String) ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");
    @Override
    public List<Integer> getWaringLevelValue() {
        String url ="http://"+Ip+":"+Port+"/api/v1/alarm";
        String da= InfUtil.interfaceUtil(url,"");
        JSONObject a = new JSONObject(da);
        JSONArray array = a.getJSONArray("alarmList");
        List<Integer> list =new ArrayList<>();
        int prompt=0;
        int lower=0;
        int middle=0;
        int high=0;
        int emergent=0;
        for (int i = 0; i < array.length(); i++) {
            JSONObject ob  = (JSONObject) array.get(i);
            Integer level = ob.getInt("level");
            if (1==level){
                prompt++;
            }else if(2==level) {
                lower++;
            }else if(3==level) {
                middle++;
            }else if(4==level) {
                high++;
            }else if(5==level) {
                emergent++;
            }
        }
        list.add(prompt);
        list.add(lower);
        list.add(middle);
        list.add(high);
        list.add(emergent);
        return list;
    }
}
